package com.example.accountbook;

import android.accounts.AccountsException;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import java.util.ArrayList;
import java.util.List;

public class MainAccountFragment extends Fragment {//账目列表碎片

    private ViewPager viewPager;
    private RadioGroup rgSelect;

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.main_account,container,false);
        viewPager = view.findViewById(R.id.vp_list_account);//获取滑动页控件
        rgSelect = view.findViewById(R.id.rg_select_account);//获取单选按钮组控件
        List<Fragment> fragmentList = new ArrayList<>();//创建元素为fragment的list

        fragmentList.add(new AccountAllFragment());//为list添加元素
        fragmentList.add(new AccountEatFragment());
        fragmentList.add(new AccountClothesFragment());
        fragmentList.add(new AccountTrafficFragment());
        fragmentList.add(new AccountPlayFragment());

        AccountFragAdapter adapter = new AccountFragAdapter(getChildFragmentManager(),fragmentList);//创建自定义碎片滑动适配器，传入子容器碎片管理对象（fragment嵌套使用），和list

        viewPager.setAdapter(adapter);//给滑动页控件添加适配器
        rgSelect.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {//给单选按钮组控件注册选择事件
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {//传入单选按钮组和选中位置
                switch (checkedId) {

                    case R.id.rb_all_account://单选按钮id判断
                        viewPager.setCurrentItem(0);//用滑动页控件调用，修改当前显示项方法，传入索引位置
                        break;

                    case R.id.rb_eat_account:
                        viewPager.setCurrentItem(1);
                        break;

                    case R.id.rb_clothes_account:
                        viewPager.setCurrentItem(2);
                        break;

                    case R.id.rb_traffic_account:
                        viewPager.setCurrentItem(3);
                        break;

                    case R.id.rb_play_account:
                        viewPager.setCurrentItem(4);
                        break;
                    default:
                        break;
                }
            }
        });

        return view;//返回设置好的视图
    }
}
